<?php

/**
 * @author dungdhs@gmail.com
 * @copyright 2012
 */

function fb_profile_picture($uid, $width=600, $height=600){
  $url = 'https://graph.facebook.com/%s/picture?height=%d&width=%d';
  return fb_download_image(sprintf($url, $uid, $width, $height));
}

function fb_download_image($url){
  $ch = curl_init ($url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
  $rawdata=curl_exec($ch);
  curl_close ($ch);
  
  return @imagecreatefromstring($rawdata);
}

function fb_add_text(&$im, $string){
  
  // try changing this as well
  $font = 2;
  $width = imagefontwidth($font) * strlen($string) + 6;
  $height = imagefontheight($font);
  $textImg = imagecreatetruecolor($width, $height);
  
  $x = imagesx($im) - $width - 3;
  $y = imagesy($im) - $height - 3;
  $backgroundColor = imagecolorallocate ($textImg, 255, 255, 255);
  $textColor = imagecolorallocate ($textImg, 0, 0, 0);
  imagefill($textImg, 0, 0, $backgroundColor);
  imagestring ($textImg, $font, 3, 0,  $string, $textColor);
  
  imagecopyresampled($im, $textImg, $x, $y, 0, 0, $width, $height, $width, $height);
}

function fb_save_image($img){
  
}